package com.aaa.controller;

import com.aaa.entity.Order;
import com.aaa.entity.Product;
import com.aaa.feign.ProductFeign;
import com.aaa.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    // 在nacos中封装了一个类DiscoverClient,该类可以获取注册中心中指定的清单列表
    @Autowired
    private DiscoveryClient discoveryClient;
    @Autowired
    private OrderService orderService;
    // 使用openfeign实现远程调用
    @Autowired(required = false)
    private ProductFeign productFeign;
    @RequestMapping("/saveOrder")
    public Order saveOrder(Long pid,Integer num){
        // jvm使用动态代理生成代理类，注入，从而实现远程调用
        System.out.println(productFeign);

        Order order = new Order();
        // 存入用户信息  session jwt
        order.setUid(1);
        order.setUsername("张三");
        Product shopInfo = productFeign.getShopInfo(pid);
        order.setPid(pid);
        order.setPname(shopInfo.getPname());
        order.setPprice(shopInfo.getPprice());
        order.setNumber(shopInfo.getStock());
        return order;
    }


    //@RequestMapping("/saveOrder")
    public String saveOrder01(Long pid,Integer num){
        Order order = new Order();
        // 存入用户信息  session jwt
        order.setUid(1);
        order.setUsername("张三");

        // 用过pid远程调用获取到商品信息，存入进去，在保存到数据库当中
        Product forObject = restTemplate.getForObject("http://shop-product/product/get/" + pid, Product.class);
        order.setPid(pid);
        order.setPname(forObject.getPname());
        order.setPprice(forObject.getPprice());
        order.setNumber(forObject.getStock());

        //orderService.insert(order);
        return "订单添加成功";
    }

    // 保存订单信息  哪个用户买了哪个商品买了多少
    //@RequestMapping("/saveOrder")
    public String saveOrder(Integer num,Long pid){
        Order order = new Order();
        // 存入用户信息  session jwt
        order.setUid(1);
        order.setUsername("张三");
        // 因为可能获取到的实例是一个集群，通过下标来获取第几个服务
        /* 使用随机数来实现负载均衡 ,随机数导致获得的uri的端口号不同 */
        List<ServiceInstance> instances = discoveryClient.getInstances("shop-product");
        // int i = new Random().nextInt(instances.size());
        ServiceInstance serviceInstance = instances.get(0);
        discoveryClient.getInstances("shop-product");
        String host = serviceInstance.getHost();
        System.out.println("host = " + host);
        int port = serviceInstance.getPort();
        System.out.println("port = " + port);
        String serviceId = serviceInstance.getServiceId();
        System.out.println("serviceId = " + serviceId);
        URI uri = serviceInstance.getUri();
        System.out.println("uri = " + uri);
        /* ribbin实现负载均衡 */


        // 用过pid远程调用获取到商品信息，存入进去，在保存到数据库当中
        Product forObject = restTemplate.getForObject("http://shop-product/product/get/" + pid, Product.class);
        order.setPid(pid);
        order.setPname(forObject.getPname());
        order.setPprice(forObject.getPprice());
        order.setNumber(forObject.getStock());
        System.out.println("forObject = " + forObject);
        //orderService.insert(order);
        return "订单添加成功:服务的端口号是：" + port;
    }
}
